﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Model;
using Besture.Utils;
using Besture.Common.Definition;

namespace Besture.UI.Admin.Service
{
    public partial class RiskConfigSet : BasePage
    {


        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            if (!IsPostBack)
            {
                bindIndustry();
                bindIncomeSource();
                bindAlimonyUse();
                bindOnceInvest();
                bindAttentionInvest();
                bindInvestSource();

                if (Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    //编辑编号
                    hidEditID.Value = Request.QueryString["id"];
                    if (!Fun.IsNumber(hidEditID.Value))
                    {
                        RedirectErrorUrl("编号错误");
                    }
                    InitData(Convert.ToInt32(hidEditID.Value));
                }
            }
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            RiskConfigDac dac = new RiskConfigDac();
            RiskConfigModel model = dac.GetModel(id);
            txtMobile.Text = model.Mobile != null ? model.Mobile.ToString() : "";
            txtName.Text = model.Name != null ? model.Name.ToString() : "";
            txtHealthStatus.Text = model.HealthStatus != null ? model.HealthStatus.ToString() : "";
            txtPreference.Text = model.Preference != null ? model.Preference.ToString() : "";
            ddlIndustry.SelectedValue = model.Industry.ToString();
            rbtnIsHasCom.SelectedValue = model.IsHasCom.ToString();
            var allIncomeSource = model.IncomeSource.Split(',');
            for (int i = 0; i < allIncomeSource.Length; i++)
            {
                for (int j = 0; j < ckbIncomeSource.Items.Count; j++)
                {
                    if (allIncomeSource[i] == ckbIncomeSource.Items[j].Value)
                        ckbIncomeSource.Items[i].Selected = true;
                }
            }
            txtAlimony.Text = model.Alimony != null ? model.Alimony.ToString() : "";
            ddlAlimonyUse.SelectedValue = model.AlimonyUse.ToString();
            var allOnceInvest = model.OnceInvest.Split(',');
            for (int i = 0; i < allOnceInvest.Length; i++)
            {
                for (int j = 0; j < ckbOnceInvest.Items.Count; j++)
                {
                    if (allOnceInvest[i] == ckbOnceInvest.Items[j].Value)
                        ckbOnceInvest.Items[i].Selected = true;
                }
            }
            var allAttentionInvest = model.AttentionInvest.Split(',');
            for (int i = 0; i < allAttentionInvest.Length; i++)
            {
                for (int j = 0; j < ckbAttentionInvest.Items.Count; j++)
                {
                    if (allAttentionInvest[i] == ckbAttentionInvest.Items[j].Value)
                        ckbAttentionInvest.Items[i].Selected = true;
                }
            }
            var allInvestSource = model.InvestSource.Split(',');
            for (int i = 0; i < allInvestSource.Length; i++)
            {
                for (int j = 0; j < ckbInvestSource.Items.Count; j++)
                {
                    if (allInvestSource[i] == ckbInvestSource.Items[j].Value)
                        ckbInvestSource.Items[i].Selected = true;
                }
            }
            txtStartFund.Text = model.StartFund != null ? model.StartFund.ToString() : "";
            txtYearInvest.Text = model.YearInvest != null ? model.YearInvest.ToString() : "";
            txtEndTime.Text = model.EndTime != null ? model.EndTime.ToString() : "";
            txtDescription.Text = model.Description != null ? model.Description.ToString() : "";
            txtTargetFund.Text = model.TargetFund != null ? model.TargetFund.ToString() : "";
            txtTargetDescription.Text = model.TargetDescription != null ? model.TargetDescription.ToString() : "";
            txtYearIn.Text = model.YearIn != null ? model.YearIn.ToString() : "";
            txtYearOut.Text = model.YearOut != null ? model.YearOut.ToString() : "";
            txtMySalaryIn.Text = model.MySalaryIn != null ? model.MySalaryIn.ToString() : "";
            txtMySalaryOut.Text = model.MySalaryOut != null ? model.MySalaryOut.ToString() : "";
            txtWifeSalaryIn.Text = model.WifeSalaryIn != null ? model.WifeSalaryIn.ToString() : "";
            txtMedicalOut.Text = model.MedicalOut != null ? model.MedicalOut.ToString() : "";
            txtBonusIn.Text = model.BonusIn != null ? model.BonusIn.ToString() : "";
            txtEducationOut.Text = model.EducationOut != null ? model.EducationOut.ToString() : "";
            txtInterestIn.Text = model.InterestIn != null ? model.InterestIn.ToString() : "";
            txtSafeOut.Text = model.SafeOut != null ? model.SafeOut.ToString() : "";
            txtRentIn.Text = model.RentIn != null ? model.RentIn.ToString() : "";
            txtOtherOut.Text = model.OtherOut != null ? model.OtherOut.ToString() : "";
            txtOtherIn.Text = model.OtherIn != null ? model.OtherIn.ToString() : "";
            txtYearOutRate.Text = model.YearOutRate != null ? model.YearOutRate.ToString() : "";
            txtYearInRate.Text = model.YearInRate != null ? model.YearInRate.ToString() : "";
            txtAssets.Text = model.Assets != null ? model.Assets.ToString() : "";
            txtLiabilities.Text = model.Liabilities != null ? model.Liabilities.ToString() : "";
            txtCash.Text = model.Cash != null ? model.Cash.ToString() : "";
            txtFundLoans.Text = model.FundLoans != null ? model.FundLoans.ToString() : "";
            txtFixedIn.Text = model.FixedIn != null ? model.FixedIn.ToString() : "";
            txtPersonalLoan.Text = model.PersonalLoan != null ? model.PersonalLoan.ToString() : "";
            txtFloatingIn.Text = model.FloatingIn != null ? model.FloatingIn.ToString() : "";
            txtHousingLoans.Text = model.HousingLoans != null ? model.HousingLoans.ToString() : "";
            txtRealEstate.Text = model.RealEstate != null ? model.RealEstate.ToString() : "";
            txtOtherDebt.Text = model.OtherDebt != null ? model.OtherDebt.ToString() : "";
            txtNoRealEstate.Text = model.NoRealEstate != null ? model.NoRealEstate.ToString() : "";
            txtInsurance.Text = model.Insurance != null ? model.Insurance.ToString() : "";
            txtOtherAssets.Text = model.OtherAssets != null ? model.OtherAssets.ToString() : "";
            txtAssetsScale.Text = model.AssetsScale != null ? model.AssetsScale.ToString() : "";

        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            RiskConfigDac dac = new RiskConfigDac();
            RiskConfigModel model = null;
            string editID = hidEditID.Value;
            //添加
            if (string.IsNullOrEmpty(editID))
            {
                model = new RiskConfigModel();
                model.Mobile = txtMobile.Text.Trim();
                model.Name = txtName.Text.Trim();
                model.HealthStatus = txtHealthStatus.Text.Trim();
                model.Preference = txtPreference.Text.Trim();
                model.Industry = ddlIndustry.SelectedValue != "" ? Convert.ToInt32(ddlIndustry.SelectedValue) : 0;
                model.IsHasCom = rbtnIsHasCom.SelectedValue != "" ? Convert.ToInt32(rbtnIsHasCom.SelectedValue) : 0;
                string allck = "";
                for (int i = 0; i < ckbIncomeSource.Items.Count; i++)
                {
                    if (ckbIncomeSource.Items[i].Selected)
                        allck += ckbIncomeSource.Items[i].Value + ",";
                }
                allck = allck.TrimEnd(',');
                model.IncomeSource = allck;
                model.Alimony = txtAlimony.Text.Trim();
                model.AlimonyUse = ddlAlimonyUse.SelectedValue != "" ? Convert.ToInt32(ddlAlimonyUse.SelectedValue) : 0;
                allck = "";
                for (int i = 0; i < ckbOnceInvest.Items.Count; i++)
                {
                    if (ckbOnceInvest.Items[i].Selected)
                        allck += ckbOnceInvest.Items[i].Value + ",";
                }
                allck = allck.TrimEnd(',');
                model.OnceInvest = allck;
                allck = "";
                for (int i = 0; i < ckbAttentionInvest.Items.Count; i++)
                {
                    if (ckbAttentionInvest.Items[i].Selected)
                        allck += ckbAttentionInvest.Items[i].Value + ",";
                }
                allck = allck.TrimEnd(',');
                model.AttentionInvest = allck;
                allck = "";
                for (int i = 0; i < ckbInvestSource.Items.Count; i++)
                {
                    if (ckbInvestSource.Items[i].Selected)
                        allck += ckbInvestSource.Items[i].Value + ",";
                }
                allck = allck.TrimEnd(',');
                model.InvestSource = allck;
                model.StartFund = txtStartFund.Text.Trim();
                model.YearInvest = txtYearInvest.Text.Trim();
                model.EndTime = txtEndTime.Text.Trim() != "" ? Convert.ToDateTime(txtEndTime.Text.Trim()) : DateTime.Now;
                model.Description = txtDescription.Text.Trim();
                model.TargetFund = txtTargetFund.Text.Trim();
                model.TargetDescription = txtTargetDescription.Text.Trim();
                model.YearIn = txtYearIn.Text.Trim();
                model.YearOut = txtYearOut.Text.Trim();
                model.MySalaryIn = txtMySalaryIn.Text.Trim();
                model.MySalaryOut = txtMySalaryOut.Text.Trim();
                model.WifeSalaryIn = txtWifeSalaryIn.Text.Trim();
                model.MedicalOut = txtMedicalOut.Text.Trim();
                model.BonusIn = txtBonusIn.Text.Trim();
                model.EducationOut = txtEducationOut.Text.Trim();
                model.InterestIn = txtInterestIn.Text.Trim();
                model.SafeOut = txtSafeOut.Text.Trim();
                model.RentIn = txtRentIn.Text.Trim();
                model.OtherOut = txtOtherOut.Text.Trim();
                model.OtherIn = txtOtherIn.Text.Trim();
                model.YearOutRate = txtYearOutRate.Text.Trim();
                model.YearInRate = txtYearInRate.Text.Trim();
                model.Assets = txtAssets.Text.Trim();
                model.Liabilities = txtLiabilities.Text.Trim();
                model.Cash = txtCash.Text.Trim();
                model.FundLoans = txtFundLoans.Text.Trim();
                model.FixedIn = txtFixedIn.Text.Trim();
                model.PersonalLoan = txtPersonalLoan.Text.Trim();
                model.FloatingIn = txtFloatingIn.Text.Trim();
                model.HousingLoans = txtHousingLoans.Text.Trim();
                model.RealEstate = txtRealEstate.Text.Trim();
                model.OtherDebt = txtOtherDebt.Text.Trim();
                model.NoRealEstate = txtNoRealEstate.Text.Trim();
                model.Insurance = txtInsurance.Text.Trim();
                model.OtherAssets = txtOtherAssets.Text.Trim();
                model.AssetsScale = txtAssetsScale.Text.Trim();
                model.CreateTime = DateTime.Now;
                model.IsDeleted = 0;
                model.UpdateTime = DateTime.Now;
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(Convert.ToInt32(editID));
                model.Mobile = txtMobile.Text.Trim();
                model.Name = txtName.Text.Trim();
                model.HealthStatus = txtHealthStatus.Text.Trim();
                model.Preference = txtPreference.Text.Trim();
                model.Industry = ddlIndustry.SelectedValue != "" ? Convert.ToInt32(ddlIndustry.SelectedValue) : 0;
                model.IsHasCom = rbtnIsHasCom.SelectedValue != "" ? Convert.ToInt32(rbtnIsHasCom.SelectedValue) : 0;
                string allck = "";
                for (int i = 0; i < ckbIncomeSource.Items.Count; i++)
                {
                    if (ckbIncomeSource.Items[i].Selected)
                        allck += ckbIncomeSource.Items[i].Value + ",";
                }
                allck = allck.TrimEnd(',');
                model.IncomeSource = allck;
                model.Alimony = txtAlimony.Text.Trim();
                model.AlimonyUse = ddlAlimonyUse.SelectedValue != "" ? Convert.ToInt32(ddlAlimonyUse.SelectedValue) : 0;
                allck = "";
                for (int i = 0; i < ckbOnceInvest.Items.Count; i++)
                {
                    if (ckbOnceInvest.Items[i].Selected)
                        allck += ckbOnceInvest.Items[i].Value + ",";
                }
                allck = allck.TrimEnd(',');
                model.OnceInvest = allck;
                allck = "";
                for (int i = 0; i < ckbAttentionInvest.Items.Count; i++)
                {
                    if (ckbAttentionInvest.Items[i].Selected)
                        allck += ckbAttentionInvest.Items[i].Value + ",";
                }
                allck = allck.TrimEnd(',');
                model.AttentionInvest = allck;
                allck = "";
                for (int i = 0; i < ckbInvestSource.Items.Count; i++)
                {
                    if (ckbInvestSource.Items[i].Selected)
                        allck += ckbInvestSource.Items[i].Value + ",";
                }
                allck = allck.TrimEnd(',');
                model.InvestSource = allck;
                model.StartFund = txtStartFund.Text.Trim();
                model.YearInvest = txtYearInvest.Text.Trim();
                model.EndTime = txtEndTime.Text.Trim() != "" ? Convert.ToDateTime(txtEndTime.Text.Trim()) : DateTime.Now;
                model.Description = txtDescription.Text.Trim();
                model.TargetFund = txtTargetFund.Text.Trim();
                model.TargetDescription = txtTargetDescription.Text.Trim();
                model.YearIn = txtYearIn.Text.Trim();
                model.YearOut = txtYearOut.Text.Trim();
                model.MySalaryIn = txtMySalaryIn.Text.Trim();
                model.MySalaryOut = txtMySalaryOut.Text.Trim();
                model.WifeSalaryIn = txtWifeSalaryIn.Text.Trim();
                model.MedicalOut = txtMedicalOut.Text.Trim();
                model.BonusIn = txtBonusIn.Text.Trim();
                model.EducationOut = txtEducationOut.Text.Trim();
                model.InterestIn = txtInterestIn.Text.Trim();
                model.SafeOut = txtSafeOut.Text.Trim();
                model.RentIn = txtRentIn.Text.Trim();
                model.OtherOut = txtOtherOut.Text.Trim();
                model.OtherIn = txtOtherIn.Text.Trim();
                model.YearOutRate = txtYearOutRate.Text.Trim();
                model.YearInRate = txtYearInRate.Text.Trim();
                model.Assets = txtAssets.Text.Trim();
                model.Liabilities = txtLiabilities.Text.Trim();
                model.Cash = txtCash.Text.Trim();
                model.FundLoans = txtFundLoans.Text.Trim();
                model.FixedIn = txtFixedIn.Text.Trim();
                model.PersonalLoan = txtPersonalLoan.Text.Trim();
                model.FloatingIn = txtFloatingIn.Text.Trim();
                model.HousingLoans = txtHousingLoans.Text.Trim();
                model.RealEstate = txtRealEstate.Text.Trim();
                model.OtherDebt = txtOtherDebt.Text.Trim();
                model.NoRealEstate = txtNoRealEstate.Text.Trim();
                model.Insurance = txtInsurance.Text.Trim();
                model.OtherAssets = txtOtherAssets.Text.Trim();
                model.AssetsScale = txtAssetsScale.Text.Trim();
                model.IsDeleted = 0;
                model.UpdateTime = DateTime.Now;
                dac.Update(model);
            }
            Fun.Alert("保存成功", "RiskConfigList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("RiskConfigList.aspx");
        }

        #endregion

        #region 加载绑定

        /// <summary>
        ///绑定
        /// </summary>
        public void bindIndustry()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_从事行业);
            this.ddlIndustry.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlIndustry.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindIncomeSource()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_收入来源);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbIncomeSource.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindAlimonyUse()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_赡养费用途);
            this.ddlAlimonyUse.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlAlimonyUse.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindOnceInvest()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_曾经投资产品);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbOnceInvest.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindAttentionInvest()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_投资时关注的问题);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbAttentionInvest.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindInvestSource()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_投资资讯主要来源);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbInvestSource.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }

        #endregion

    }
}
